package com.diandi.signature.manager.impl.contract;

import com.diandi.signature.bean.contract.SignatureContract;
import com.diandi.signature.bean.contract.SignatureContractDetail;
import com.diandi.signature.bean.proceessinfo.SignatureProcessDetail;
import com.diandi.signature.bean.proceessinfo.SignatureProcessLog;
import com.diandi.signature.enums.fdd.ContractStatus;
import com.diandi.signature.enums.platfrom.BusinessStatus;
import com.diandi.signature.enums.platfrom.PassStatus;
import com.diandi.signature.enums.platfrom.PlatFormStatus;
import com.diandi.signature.enums.platfrom.ProcessFlag;
import com.diandi.signature.manager.contract.FddSignatureContractManager;
import com.diandi.signature.mapper.SignatureContractDetailMapper;
import com.diandi.signature.mapper.SignaturePlatformPkgmsgMapper;
import com.diandi.signature.mapper.SignatureProcessDetailMapper;
import com.diandi.signature.mapper.SignatureProcessLogMapper;
import com.diandi.signature.utils.PackageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
* @author mwz
* @create 2017-08-31 17:19
* @version V1.0.0
* @description description
*/
@Component
public class FddSignatureContractManagerImpl implements FddSignatureContractManager {


    @Autowired
    SignaturePlatformPkgmsgMapper signaturePlatformPkgmsgMapper;
    @Autowired
    SignatureProcessLogMapper signatureProcessLogMapper;
    @Autowired
    SignatureProcessDetailMapper signatureProcessDetailMapper;
    @Autowired
    SignatureContractDetailMapper signatureContractDetailMapper;

    /**
     * 插入过程信息
     * 发送到通道消息
     * @param params
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public boolean insertProcessInfoData(SignatureContract signatureContract,Map<String, String> params,Integer processFlag) throws Exception {

        /**2.1、插入状态主表数据*/
        SignatureProcessLog signatureProcessLog =  PackageParams.getInitSignatureProcessLogtPkgMsg( params );
        signatureProcessLog.setInterfaceType( processFlag );
        signatureProcessLog.setBusinessStatus( BusinessStatus.处理中.getStatus() );
        signatureProcessLog.setPlatformStatus(PlatFormStatus.处理中.getStatus() );
        signatureProcessLog.setPassStatus( PassStatus.待受理.getStatus() );
        signatureProcessLog.setProcessFlag(ProcessFlag.处理中.getType() );
        signatureProcessLogMapper.updateSignatureProcessLog( signatureProcessLog );

        /**2.2、插入状态明细数据*/
        SignatureProcessDetail signatureProcessDetail =  PackageParams.getSignatureProcessDetailPkgMsg( params );
        signatureProcessDetail.setInterfaceType( processFlag );
        signatureProcessDetailMapper.insertSignatureProcessDetail(signatureProcessDetail);

      /*  *//**2.2、插入报文信息*//*

        params.put("requestMsg",params.toString());
        params.put("returnMsg","");
        params.put("unnotifyMsg","");
        SignaturePlatformPkgMsg signaturePlatformPkgMsg = PackageParams.getPlatFormPkgmsg( params );
        signaturePlatformPkgmsgMapper.insertSignaturePlatformPkgmsg( signaturePlatformPkgMsg );*/
        return false;
    }

    @Override
    public boolean insertProcessmTM01Data(Map<String, String> reqParamsMap,   Integer processFlag) throws Exception {
        /**2.1、插入状态主表数据*/
        SignatureProcessLog signatureProcessLog =  PackageParams.getInitSignatureProcessLogtPkgMsg( reqParamsMap );
        signatureProcessLog.setInterfaceType( processFlag );
        signatureProcessLog.setBusinessStatus( BusinessStatus.处理中.getStatus() );
        signatureProcessLog.setPlatformStatus(PlatFormStatus.处理中.getStatus() );
        signatureProcessLog.setPassStatus( PassStatus.待受理.getStatus() );
        signatureProcessLog.setProcessFlag(ProcessFlag.处理中.getType() );
        signatureProcessLogMapper.updateSignatureProcessLog( signatureProcessLog );

        /**2.2、插入状态明细数据*/
        SignatureProcessDetail signatureProcessDetail =  PackageParams.getSignatureProcessDetailPkgMsg( reqParamsMap );
        signatureProcessDetail.setInterfaceType( processFlag );
        signatureProcessDetailMapper.insertSignatureProcessDetail(signatureProcessDetail);


        return true;
    }



    /**
     * 更新中间数据状态
     * @param signatureContract
     * @param params
     * @param processFlag
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public SignatureContract updateProcessInfoData(SignatureContract signatureContract,Map<String, String> params,Integer processFlag ) throws Exception {

        /**1.2、插入合同明细*/

        SignatureContractDetail signatureContractDetail =  PackageParams.getSignatureContractDetailPkgMsg( params );
        signatureContractDetailMapper.insertSignatureContractDetail( signatureContractDetail );

        /**1.1、更新中间状态数据信息*/
        SignatureContract contract = new SignatureContract();
        Map<String, String> queryParams = new HashMap<String,String>();
        queryParams.put("signatureCode",signatureContract.getSignatureCode() );
        queryParams.put("contractCode",signatureContract.getContractCode() );
        queryParams.put("userId",   String.valueOf( signatureContract.getUserId() ) );
        SignatureProcessLog  signatureProcessLog = signatureProcessLogMapper.getSignatureProcessLogByCode(queryParams);
        signatureProcessLog.setInterfaceType( processFlag );
        signatureProcessLog.setBusinessStatus(BusinessStatus.处理中.getStatus());
        signatureProcessLog.setPlatformStatus(PlatFormStatus.处理中.getStatus());
        signatureProcessLog.setPassStatus( PassStatus.待受理.getStatus());
        signatureProcessLog.setProcessFlag(ProcessFlag.处理完成.getType() );
        signatureProcessLogMapper.updateSignatureProcessLog( signatureProcessLog );

        /**2.2、插入状态明细数据*/
        SignatureProcessDetail signatureProcessDetail =  PackageParams.getUpdateProcessDetailPkgMsg( params );
        signatureProcessDetail.setInterfaceType( processFlag );
        signatureProcessDetailMapper.insertSignatureProcessDetail(signatureProcessDetail);



        return contract;

    }

    @Override
    public boolean updateProcessmTM01Data( Map<String,String> paramMap, Map<String, String> reqParamsMap,   Map<String, String> rtnParamsMap, Integer processFlag) throws Exception {
        /**1.2、插入合同明细*/

        SignatureContractDetail signatureContractDetail =  PackageParams.getSignatureTM01PkgMsg( paramMap,reqParamsMap,rtnParamsMap, ContractStatus.合同签章完成.getType());
        signatureContractDetailMapper.insertSignatureContractDetail( signatureContractDetail );

        /**1.1、更新中间状态数据信息*/
        SignatureContract contract = new SignatureContract();
        Map<String, String> queryParams = new HashMap<String,String>();
        queryParams.put("signatureCode",paramMap.get("signatureCode") );
        queryParams.put("contractCode",paramMap.get("contractCode"));
        queryParams.put("userId",   paramMap.get("userId") );
        SignatureProcessLog  signatureProcessLog = signatureProcessLogMapper.getSignatureProcessLogByCode(queryParams);
        signatureProcessLog.setInterfaceType( processFlag );
        signatureProcessLog.setBusinessStatus(BusinessStatus.处理中.getStatus());
        signatureProcessLog.setPlatformStatus(PlatFormStatus.处理中.getStatus());
        signatureProcessLog.setPassStatus( PassStatus.待受理.getStatus());
        signatureProcessLog.setProcessFlag(ProcessFlag.处理完成.getType() );
        signatureProcessLogMapper.updateSignatureProcessLog( signatureProcessLog );

        /**2.2、插入状态明细数据*/
        SignatureProcessDetail signatureProcessDetail =  PackageParams.getUpdateProcessDetailPkgMsg( paramMap );
        signatureProcessDetail.setInterfaceType( processFlag );
        signatureProcessDetailMapper.insertSignatureProcessDetail(signatureProcessDetail);

        return true;
    }


}
